function TRW=bciCompareFeatures(dat,label, method, C, cond)
% bciCompareFeatures(dat,label, method, C)
% Wrapper function for pairwise comparisons of features
% INPUT:
%   dat - two dimensional data of size (nSamples X nFeatures)
%   label - vector of length size(dat,1) including 2 different values
%   method - string of comparison method:
%             'tval' - t-values
%             'rsqu' - Rsquare values
%             'svm' - weights of a support vector machine
%             'swlr' - stepwise linear regression (requires stats toolbox)
%   C (optional) - svm regularization parameter (default: Joachims)
%   cond (optional) - two element vector of conditions (default: label
%                                                                elements)
% OUTPUT: 
%   TRW - vector of Tvalues, Rsquare values, svm Weights, respectively
%
% CR wrote it 2011/06/07

if nargin<5,
    cond=unique(label);
end

if length(cond)~=2,
    error('Only comparisons of two conditions supported.');
end
if ndims(dat)>2,
    error('Only comparisons of two dimensional data supported.');
end

if strfind(lower(method),'tval'),
    D1.data=permute(dat(label==cond(1),:),[2 3 1]);
    D2.data=permute(dat(label==cond(2),:),[2 3 1]);
    TRW = ecogTValues(D1,D2)';
elseif strfind(lower(method),'rsqu'),
    TRW = ecogRsquare(dat(label==cond(1),:),dat(label==cond(2),:),1);
elseif strfind(lower(method),'svm'),
    L=zeros(size(label));
    L(label==cond(1))=1;
    L(label==cond(2))=-1;
    % train the classifier
    d = data(dat,L);
    alg = svm;
    alg.optimizer='libsvm';
    alg.algorithm.verbosity=0;
    if nargin<4 || isempty(C),
        alg.C = getDefC(dat);
    else
        alg.C = C;
    end
    [tr alg] = train(alg,d);
    TRW = get_w(alg);
elseif strfind(lower(method),'swlr'),
    [b,se,pval,inmodel] = stepwisefit(dat,label,'penter',0.1,'premove',0.15,'display','off','maxiter',64);
    TRW=double(inmodel)+(1-pval').*double(~inmodel);
else
    error('unknown comparison method ');
end